package com.zmx.doctor.view.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.wang.adapters.interfaceabstract.OnItemClickListener;
import com.zmx.doctor.BaseActivity;
import com.zmx.doctor.R;
import com.zmx.doctor.adapter.PatientsAdapter;
import com.zmx.doctor.okhttp.OkHttp3ClientManager;
import com.zmx.doctor.pojo.Patients;
import com.zmx.doctor.utils.AllUtil;
import com.zmx.doctor.view.util.MySharedPreferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PatientMessageActivity extends BaseActivity {

    @BindView(R.id.layout1)
    RelativeLayout layout1;
    @BindView(R.id.layout2)
    RelativeLayout layout2;
    @BindView(R.id.layout3)
    RelativeLayout layout3;
    @BindView(R.id.layout4)
    RelativeLayout layout4;
    private PatientsAdapter adapter;
    private List<Patients> lists;
    private RecyclerView reView;
    private TextView p_number;
    private String s_name, s_head, patient_id;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_patient_message;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        setToolbar(R.id.tool_bar);

        lists = new ArrayList<>();
        adapter = new PatientsAdapter(this);
        reView = findViewById(R.id.reView);
        reView.setLayoutManager(new LinearLayoutManager(this));
        reView.setAdapter(adapter);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            protected void onItemClick(View view, int listPosition) {

                s_head = lists.get(listPosition).getAvatar();
                s_name = lists.get(listPosition).getName();
                patient_id = lists.get(listPosition).getPatient_id() + "";
                getPulesStatus();

            }
        });
        p_number = findViewById(R.id.p_number);


        getPtient();
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {

                case 1:

                    try {

                        JSONObject object = new JSONObject(msg.obj.toString());
                        JSONArray array = object.getJSONArray("data");
                        Gson gson = new Gson();
                        for (int i = 0; i < array.length(); i++) {

                            Patients p = gson.fromJson(array.getJSONObject(i).toString(), Patients.class);
                            lists.add(p);

                        }
                        p_number.setText("全部患者" + lists.size() + "名");
                        adapter.setListAndNotifyDataSetChanged(lists);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                    break;

                case 10:

                    try {

                        JSONObject jsonObject = new JSONObject(msg.obj.toString());


                        if (jsonObject.getInt("code") == 1) {

                            Bundle bundles = new Bundle();
                            bundles.putString("name", s_name);
                            bundles.putString("head", s_head);
                            bundles.putString("p_id", patient_id);
                            bundles.putString("data", jsonObject.getString("data"));
                            startActivity(PulseConditionActivity.class, bundles);

                        } else {

                            Toast(jsonObject.getString("msg"));

                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;


            }

        }
    };

    public void getPtient() {

        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(null)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "getPtient");
        params.put("data[]", "");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/doctor/", params, handler, 1, 404);

    }


    @OnClick({R.id.layout1, R.id.layout2, R.id.layout3, R.id.layout4})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout1:

                startActivity(AddPatientsActivity.class);

                break;
            case R.id.layout2:

                Intent intent = new Intent();
                intent.setClass(mActivity, GroupingActivity.class);
                startActivity(intent);
                break;
            case R.id.layout3:

                Toast("没有患者标签！");
                break;
            case R.id.layout4:

                startActivity(AutomaticFollowActivity.class);
                break;
        }
    }

    /**
     * 检测脉象
     */
    public void getPulesStatus() {

        List<String> ls = new ArrayList<>();
        ls.add("5");
        ls.add(patient_id);
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(ls)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "getPulesStatus");
        params.put("data[patient_id]", patient_id);
        params.put("data[minute]", "5");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/watch", params, handler, 10, 404);

    }


}
